#include "include.h"

//------------------------------------------当前位置、后一位置-------------------------------
Pos current,next;
//---------------------------------------------栈指针---------------------------------------
stack s;
//----------------------------------------------地图----------------------------------------
int Maze[10][10] ={
    {1,1,1,1,1,1,1,1,1,1},
    {1,8,0,0,0,0,0,0,0,1},
    {1,1,1,1,0,1,1,1,0,1},
    {1,0,0,0,0,1,0,1,0,1},
    {1,0,1,1,1,1,0,1,1,1},
    {1,0,0,0,0,1,0,0,0,1},
    {1,0,1,1,0,0,0,1,0,1},
    {1,0,1,1,1,0,1,1,1,1},
    {1,0,0,0,1,0,0,0,8,1},
    {1,1,1,1,1,1,1,1,1,1},
};
//--------------------------------------------初始化栈--------------------------------------
status initStack(stack *s)
{
    s->base =(SElemType *)malloc(sizeof(SElemType)*STACKSIZE);//将栈指针指向开辟的数组
    if(s->base == NULL)
    {
        exit(1);
    }
    s->top = s->base;
    s->StackSize = STACKSIZE;
    printf("init stack pass\n");
    return ok;
}
//---------------------------------------------push---------------------------------------
status push(stack *s, SElemType pos)
{
    if(s->top - s->base == s->StackSize)
    {
        return error;
    }
    else
    {
        s->top ++;
        *s->top = pos;
        printf("find next and push %d %d\n",(*s->top).x,(*s->top).y);
    }
}
//-----------------------------------------------pop--------------------------------------
SElemType pop(stack *s)
{
    SElemType e;
    if(s->top == s->base)
    {
        e.x = -1;
        e.y = -1;
        return e;
    }
    else
    {
        e = *s->top;
        s->top --;
        return e;
    }
}
//-----------------------------------------显示迷宫----------------------------------------
void ShowMaze(int (*Maze)[10])
{
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (Maze[i][j] == 0)        {printf(" - ");}
            else if (Maze[i][j] == 1)   {printf(" * ");}
            else if(Maze[i][j] == 2)    {printf(" > ");}    //走过的路
            else if(Maze[i][j] == 3)    {(" ? ");}          //死胡同printf
            else if(Maze[i][j] == 8)    {printf(" @ ");}    //终点
        }
        printf("\n");
    }
}
//-----------------------------------------设置起点-------------------------------------
void setStart(stack *s)
{
    current.x = 1;
    current.y = 1;
    Maze[current.x][current.y] = 2 ;
    push(s, current); 
    ShowMaze(Maze);
}
//---------------------------------------寻找迷宫出路------------------------------------
void foundway(int (* Maze)[10],stack *s)
{
    //next = gonext(Maze,current);
    while(Maze[current.x][current.y] != 8) //还没到出口
    {
        printf("into gonext function \n");
        next = gonext(Maze,current);
        if(Maze[next.x][next.y] == 8)
        {
            break;
        }
        else if(Maze[next.x][next.y] != 2) //不是死胡同
        {
            current = next; //向前走一步
            Maze[current.x][current.y] = 2; //标记一下走过的路，避免重复走
            push(s,current);
            ShowMaze(Maze);
        }
        else  //死胡同
        {
            Maze[current.x][current.y] = 3; //标记一下走不通的路
            pop(s);
            current = *s->top;
            printf("pop! current is %d %d\n",current.x,current.y);
            ShowMaze(Maze);
        }
    }   
}
//-------------------------------------右、下、左、后-----------------------------------
Pos gonext(int (* maze)[10],Pos current)
{
    Pos temp = current;
    if(maze[current.x][current.y + 1] == 0 || maze[current.x][current.y + 1] == 8 )         {temp.y = current.y + 1;}
    else if(maze[current.x + 1][current.y ] == 0 || maze[current.x + 1][current.y ] == 8)   {temp.x = current.x + 1;}
    else if(maze[current.x][current.y - 1] == 0 || maze[current.x][current.y - 1] == 8)     {temp.y =current.y - 1;}
    else if(maze[current.x - 1][current.y] == 0 || maze[current.x - 1][current.y] == 8)     {temp.x = current.x - 1;}
    else if(maze[current.x][current.y + 1] == 2)    { temp.y = current.y + 1;}
    else if(maze[current.x + 1][current.y ] == 2)   { temp.x = current.x + 1;}
    else if(maze[current.x][current.y - 1] == 2)    { temp.y =current.y - 1;}
    else if(maze[current.x - 1][current.y] == 2)    { temp.x = current.x - 1;}
    printf("findnext is x=%d y=%d\n",temp.x,temp.y);
    return temp;
}